var path = require('path')
var webpack = require('webpack')
var fs = require('fs')
var srcDir = path.join(process.cwd(), 'client')
var buildDir = path.join(process.cwd(), 'build')
var nodeModulesPath = path.join(process.cwd(), 'nodeModulesPath')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var autoprefixer = require('autoprefixer')
var pxtorem = require('postcss-pxtorem')
var bundleConfig = require('./build/bundle-config.json')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var imagePath = path.join(srcDir, 'img')
var cssPath = path.join(srcDir, 'css')
var port = 8001;

module.exports = {
    cache: true,
    entry: {
        index: [
            path.join(srcDir, 'views/index.jsx')
        ]
    },
    output: {
        publicPath: '/',
        path: path.join(buildDir, 'static/'), 
        filename: 'core/[name].js', 
        chunkFilename: 'core/[name].js'
    },
    resolve: {
        extensions: ['.js', '.jsx', '.json'],
        alias: {
            'img': path.join(srcDir, 'img'),
            'css': path.join(srcDir, 'css'),
            'js': path.join(srcDir, 'js'),
            'views': path.join(srcDir, 'views')
        }
    },
    module: {
        rules: [{
            test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: 'url-loader?name=img/[name].[hash:5].[ext]',
        },
        {
            test: /\.(scss|sass|css)$/,
            use: ['style-loader',
                {
                    loader: 'css-loader',
                    options: {
                        sourceMap: true
                    }
                },
                {
                    loader: 'postcss-loader',
                    options: {
                        sourceMap: true,
                        plugins: [autoprefixer, pxtorem({
                            rootValue: 50,
                            minPixelValue: 1,
                            propWhiteList: [],
                        })]
                    }
                },
                {
                    loader: 'sass-loader',
                    options: {
                        sourceMap: true,
                        imagePath: imagePath,
                        includePaths: [cssPath]
                    }
                }
            ]
        },
        {
            test: /\.(js|jsx)$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: 'babel-loader',
        }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./build/static/dll/vendor-manifest.json')
        }),
        new CopyWebpackPlugin([{
            from: path.join(srcDir, '/js/lib'),
            to: path.join(buildDir, '/static/lib')
        }]),
        new HtmlWebpackPlugin({
            template: path.join(srcDir, 'html/index.html'),
            filename: path.join(buildDir, 'html/index.html'),
            bundleName: bundleConfig.vendor.js, // 打包生成的文件路径
            inject: true
        })
    ]
};
